﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using MapControl;
using MapControl.Misc;

namespace GPSMap
{
    public partial class frmGPSTracking : Form
    {
        private ComPort comport;
        public MapControl.ucMap_2 mapControl;
        private frmTestCOMPort frmTest;// = new frmTestCOMPort(comport);
        public frmGPSTracking()
        {
            InitializeComponent();
            //Load Map            
            mapControl = new ucMap_2();
            panelMap.Controls.Add(mapControl);
            mapControl.Dock = DockStyle.Fill;
            
            mapControl.AxMap_MouseMoveEvent += new AxMapXLib.CMapXEvents_MouseMoveEventHandler(AxMap_MouseMoveEvent);
            mapControl.AxMap_MouseWheelEvent += new AxMapXLib.CMapXEvents_MouseWheelEventHandler(AxMap1_MouseWheelEvent);
        }

        public void AxMap_MouseMoveEvent(object sender, AxMapXLib.CMapXEvents_MouseMoveEvent e)
        {
            UpdateToolStrip(e.x, e.y);
        }

        private void AxMap1_MouseWheelEvent(object sender, AxMapXLib.CMapXEvents_MouseWheelEvent e)
        {
            UpdateToolStrip(e.x, e.y);
        }

        private ImageList imgList = new ImageList();
        public void ToolStripSetup()
        {
            //// Create the ImageList
            
            imgList.ImageSize = new Size(18, 16);
            imgList.ColorDepth = ColorDepth.Depth4Bit;
            imgList.TransparentColor = Color.FromArgb(192, 192, 192);

            // adds the bitmap
            if (File.Exists("buttons.bmp"))
            {
                //path = System.IO.Path.GetFullPath("buttons.bmp");
                //bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(this.GetType(), "buttons.bmp"));
                Bitmap bmp = new Bitmap("buttons.bmp");
                imgList.Images.AddStrip(bmp);
            }

            toolStripButtonSelectTool.Image = imgList.Images[9];
            toolStripButtonPanTool.Image = imgList.Images[18];
            toolStripButtonZoomIn.Image = imgList.Images[15];
            toolStripButtonZoomOut.Image = imgList.Images[16];
        }

        private void toolStripButtonSelectTool_Click(object sender, EventArgs e)
        {
            mapControl.Map.CurrentTool = MapXLib.ToolConstants.miArrowTool;
        }

        private void toolStripButtonPanTool_Click(object sender, EventArgs e)
        {
            mapControl.Map.CurrentTool = MapXLib.ToolConstants.miPanTool;
        }

        private void toolStripButtonZoomIn_Click(object sender, EventArgs e)
        {
            mapControl.Map.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
        }

        private void toolStripButtonZoomOut_Click(object sender, EventArgs e)
        {
            mapControl.Map.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
        }

        private void testDữLiệuGPSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmTestGPSData frm = new frmTestGPSData(this);
            //frm.Location = new Point(this.Location.X + this.Width - frm.Size.Width, frm.Location.Y);
            frm.Location = new Point(300, frm.Location.Y);
            frm.Show();
        }

        private void frmGPSTracking_Load(object sender, EventArgs e)
        {
            comport = new ComPort(Properties.Settings.Default.PortName, Properties.Settings.Default.BaudRate, Properties.Settings.Default.StopBits, Properties.Settings.Default.DataBits, Properties.Settings.Default.Parity, true);
            comport.BytesToRead = 47;
            frmTest = new frmTestCOMPort(comport);

            string[] bigSquareArray = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            mapControl.Coord9x9.BigSquareArray = bigSquareArray;
            ToolStripSetup();
        }

        public void UpdateToolStrip(float x, float y)
        {
            PointF pScreenF = new PointF(x, y);
            //Hiển thị tọa độ Longitude, Latitude
            double lon, lat;
            lon = lat = 0;
            mapControl.Map.ConvertCoord(ref x, ref y, ref lon, ref lat, MapXLib.ConversionConstants.miScreenToMap);
            //toolStripStatusLabelCoordinate.Text = string.Format("K:{0} | V:{1} | Độ cao: {2}", GeneralClass.ConvertDecimalToDMSCoordinate(lon), GeneralClass.ConvertDecimalToDMSCoordinate(lat), mapControl.Map.Zoom.ToString("#,# m"));
            toolStripStatusLabelCoordinate.Text = string.Format("K:{0} | V:{1}", GeneralClass.DecimalToDMS(lon), GeneralClass.DecimalToDMS(lat));
            toolStripStatusLabelAltitude.Text = string.Format("Độ cao: {0}", mapControl.Map.Zoom.ToString("#,# m"));
            toolStripStatusLabelCoord9x9.Text = string.Format("Tọa độ 9x9: {0}", mapControl.Coord9x9.GetCoordinate(pScreenF));
        }

        private void tọaĐộ9x9ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mapControl.ShowCoordinate9x9 = !mapControl.ShowCoordinate9x9;
            if (mapControl.ShowCoordinate9x9)
            {
                string value = Properties.Settings.Default.Coord9x9;
                mapControl.Coord9x9.BigSquareArray = value.Split(new char[] { '|' });
                tọaĐộ9x9ToolStripMenuItem.Image = imgList.Images[20];
                mapControl.Refresh();
                mapControl.Coord9x9.Draw(mapControl.CreateGraphics());
            }
            else
            {
                tọaĐộ9x9ToolStripMenuItem.Image = null;
                mapControl.Refresh();
            }

        }

        private void địaDanhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layername = "TENTPHO";
            mapControl.Map.Layers[layername].Visible = !mapControl.Map.Layers[layername].Visible;
            //mapControl.Map.Layers["TENTPHO"].LabelProperties.Style.TextFont = ".VnArial";
            mapControl.Map.Layers[layername].AutoLabel = mapControl.Map.Layers[layername].Visible;
            if (mapControl.Map.Layers[layername].Visible)
                địaDanhToolStripMenuItem.Image = imgList.Images[20];
            else
                địaDanhToolStripMenuItem.Image = null;
        }

        private void biểnĐảoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layername = "BIENDAO";
            mapControl.Map.Layers[layername].Visible = !mapControl.Map.Layers[layername].Visible;
            //mapControl.Map.Layers["TENTPHO"].LabelProperties.Style.TextFont = ".VnArial";
            mapControl.Map.Layers[layername].AutoLabel = mapControl.Map.Layers[layername].Visible;
            if (mapControl.Map.Layers[layername].Visible)
                biểnĐảoToolStripMenuItem.Image = imgList.Images[20];
            else
                biểnĐảoToolStripMenuItem.Image = null;
        }

        private void sânBayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layername = "SANBAY";
            mapControl.Map.Layers[layername].Visible = !mapControl.Map.Layers[layername].Visible;
            if (mapControl.Map.Layers[layername].Visible)
                sânBayToolStripMenuItem.Image = imgList.Images[20];
            else
                sânBayToolStripMenuItem.Image = null;
        }

        private void cấuHìnhCổngCOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCOMSetting frm = new frmCOMSetting();
            frm.ShowDialog();
        }

        private void mởKếtNốiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Mở kết nối
            try
            {
                comport.PortName = Properties.Settings.Default.PortName;
                comport.BaudRate = Properties.Settings.Default.BaudRate;
                comport.StopBits = Properties.Settings.Default.StopBits;
                comport.DataBits = Properties.Settings.Default.DataBits;
                comport.Parity = Properties.Settings.Default.Parity;
                comport.comPortOpen();
                frmTest.Connected = true;
                toolStripStatusLabelCOMStatus.Text = string.Format("Cổng {0} đã được mở...", comport.PortName);
            }
            catch (Exception ex)
            {
                frmTest.Connected = false;
                MessageBox.Show("Xảy ra lỗi sau khi kết nối:\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void đóngKếtNốiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //đóng kết nối
            try
            {
                comport.comPortClose();
                frmTest.Connected = false;
                toolStripStatusLabelCOMStatus.Text = "Chưa kết nối...";

            }
            catch (Exception ex)
            {
                MessageBox.Show("Xảy ra lỗi sau khi kết nối:\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void testDữLiệuCổngCOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!frmTest.Connected){
                MessageBox.Show("Chưa mở cổng COM");
                return;
            }
            frmTest.Show();
            
        }

        private void cấuHìnhTọaĐộ9x9ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmConfig9x9 frm = new frmConfig9x9();
            frm.Show();
        }


        private void tỉnhThànhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layerName = "TINH";
            mapControl.Map.Layers[layerName].Visible = !mapControl.Map.Layers[layerName].Visible;
            if (mapControl.Map.Layers[layerName].Visible)
                tỉnhThànhToolStripMenuItem.Image = imgList.Images[20];
            else
                tỉnhThànhToolStripMenuItem.Image = null;
        }

        private void bảnĐồ1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layerName = "VietNam";
            mapControl.Map.Layers[layerName].Visible = !mapControl.Map.Layers[layerName].Visible;
            if (mapControl.Map.Layers[layerName].Visible)
                bảnĐồ1ToolStripMenuItem.Image = imgList.Images[20];
            else
                bảnĐồ1ToolStripMenuItem.Image = null;
        }

        private void bảnĐồ2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string layerName = "AsiaMap";
            mapControl.Map.Layers[layerName].Visible = !mapControl.Map.Layers[layerName].Visible;
            if (mapControl.Map.Layers[layerName].Visible)
                bảnĐồ2ToolStripMenuItem.Image = imgList.Images[20];
            else
                bảnĐồ2ToolStripMenuItem.Image = null;
        }


    }
}